package ink.lovejinhu.leetcode;

import org.junit.Test;

import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-04-27 9:19
 */
public class Solution_0013 {
    public static HashMap<String, Integer> map = new HashMap<>();

    static {
        map.put("I", 1);
        map.put("V", 5);
        map.put("X", 10);
        map.put("L", 50);
        map.put("C", 100);
        map.put("D", 500);
        map.put("M", 1000);


        map.put("IV", 4);
        map.put("IX", 9);
        map.put("XL", 40);
        map.put("XC", 90);
        map.put("CD", 400);
        map.put("CM", 900);
    }

    public int romanToInt(String s) {
        char[] chars = s.toCharArray();
        int i = 0;
        int result = 0;
        int length = chars.length;
        int temp = length - 2;
        while (i < length) {
            if (chars[i] == 'I') {
                if (i <= temp) {
                    if (chars[i + 1] == 'V') {
                        result = result + 4;
                        i = i + 2;
                    } else if (chars[i + 1] == 'X') {
                        result = result + 9;
                        i = i + 2;
                    } else {
                        result = result + 1;
                        i++;
                    }
                } else {
                    result = result + 1;
                    i++;
                }
            } else if (chars[i] == 'V') {
                result = result + 5;
                i++;
            } else if (chars[i] == 'X') {
                if (i <= temp) {
                    if (chars[i + 1] == 'L') {
                        result = result + 40;
                        i = i + 2;
                    } else if (chars[i + 1] == 'C') {
                        result = result + 90;
                        i = i + 2;
                    } else {
                        result = result + 10;
                        i++;
                    }
                } else {
                    result = result + 10;
                    i++;
                }
            } else if (chars[i] == 'L') {
                result = result + 50;
                i++;
            } else if (chars[i] == 'C') {
                if (i <= temp) {
                    if (chars[i + 1] == 'D') {
                        result = result + 400;
                        i = i + 2;
                    } else if (chars[i + 1] == 'M') {
                        result = result + 900;
                        i = i + 2;
                    } else {
                        result = result + 100;
                        i++;
                    }
                } else {
                    result = result + 100;
                    i++;
                }
            } else if (chars[i] == 'D') {
                result = result + 500;
                i++;
            } else if (chars[i] == 'M') {
                result = result + 1000;
                i++;
            }
        }
        return result;
    }

    @Test
    public void test() {
        String test="MCMXCIV";
        System.out.println(this.romanToInt(test));

    }

}
